﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NoteExpress;
using System.Threading;

namespace neut
{
    public partial class FolderOperation : Form
    {
        public INEFolder ParentFolder
        {
            get;
            set;
        }

        public INEFolder SelectedFolder
        {
            get;
            set;
        }

        public INEFolder CurrentFolder
        {
            get;
            set;
        }

        public FolderOperation()
        {
            InitializeComponent();
            var tmp = new NEHost2();
            CurrentFolder= tmp.Libraries.ActiveFolder;
            ParentFolder = CurrentFolder.ParentLibrary.References;
        }
        //List<Thread> _threadlist=new List<Thread>();
        TreeNodeEx current;
        void FindFolders(INEFolder folder, ref TreeNodeEx node)
        {
            for (int i = 0; i < folder.Count; i++)
            {
                var tf = folder.get_Children(i);
                var tmp = new TreeNodeEx(tf);
                node.Nodes.Add(tmp);
                if (tf.UID == CurrentFolder.UID)
                    current = tmp;

                //var thread = new Thread(new ThreadStart(() =>
                //{
                    FindFolders(tf, ref tmp);
                //}));
               // _threadlist.Add(thread);
                //thread.Start();
            }
        }

        void RefreshTree()
        {
            //Control.CheckForIllegalCrossThreadCalls = false;
            treeView1.Nodes.Clear();
            treeView1.BeginUpdate();
            var tmp = new TreeNodeEx(ParentFolder);
            treeView1.Nodes.Add(tmp);
            //_threadlist.Clear();
            FindFolders(ParentFolder, ref tmp);
            //_threadlist.ForEach((t)=>{t.Join();});
            treeView1.EndUpdate();
            treeView1.ExpandAll();
            if (current != null)
                treeView1.SelectedNode = current;
        }

        private void FolderOperation_Load(object sender, EventArgs e)
        {
            RefreshTree();
        }

        private void 选择ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SelectedFolder = (treeView1.SelectedNode as TreeNodeEx).NEFolder;
            DialogResult = DialogResult.OK;
        }

        private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            if (toolStripTextBox1.Text != "增加" && toolStripTextBox1.Text != string.Empty)
            {
                var tmp = (treeView1.SelectedNode as TreeNodeEx).NEFolder;
                var old = tmp.ParentLibrary.Parent.ActiveFolder;
                tmp.Add(toolStripTextBox1.Text);
                old.Activate();
                RefreshTree();
             }
            toolStripTextBox1.Text = "增加";
            if (toolStripTextBox2.Text != "改名" && toolStripTextBox2.Text != string.Empty)
            {   
                //throw new Exception("功能未开发");
            }
            toolStripTextBox2.Text = "改名";
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //throw new Exception("功能未开发");
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            SelectedFolder = (treeView1.SelectedNode as TreeNodeEx).NEFolder;
            DialogResult = DialogResult.OK;
        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                TreeNode tn = treeView1.GetNodeAt(e.X, e.Y);
                if (tn != null) treeView1.SelectedNode = tn;
            }   
        }

        private void 拷贝路径到剪贴板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var tmp  = treeView1.SelectedNode.FullPath;
            tmp = tmp.Substring(tmp.IndexOf(@"\")+1);
            Clipboard.SetText(tmp);
        }  
    }

    public class TreeNodeEx : TreeNode
    {
        public INEFolder NEFolder
        {
            get;
            set;
        }

        public TreeNodeEx(INEFolder folder):base(folder.Title)
        {
            NEFolder = folder;
        }
    }
}
